﻿using NetCoreCMS.Common;
using System;
using System.Collections.Generic;
using System.Text;

namespace NetCoreCMS.Common.XMLConfig
{
    public class Userconfig
    {
        #region Public
        /// <summary>
        ///  读取配置文件
        /// </summary>
        public static Models.Userconfig loadConfig(string rootPath)
        {
            Models.Userconfig model = CacheHelper.Get<Models.Userconfig>(CMSKeys.CACHE_USER_CONFIG);
            if (model == null)
            {
                CacheHelper.Set(CMSKeys.CACHE_USER_CONFIG, loadConfigXML(Utils.GetXmlMapPath(CMSKeys.FILE_USER_XML_CONFING, rootPath)));
                model = CacheHelper.Get<Models.Userconfig>(CMSKeys.CACHE_USER_CONFIG);
            }
            return model;
        }

        /// <summary>
        ///  保存配置文件
        /// </summary>
        public static Models.Userconfig saveConifg(Models.Userconfig model, string rootPath)
        {
            return saveConifgXML(model, Utils.GetXmlMapPath(CMSKeys.FILE_USER_XML_CONFING, rootPath));
        }
        #endregion
        #region Private
        private static object lockHelper = new object();

        /// <summary>
        ///  读取站点配置文件
        /// </summary>
        private static Models.Userconfig loadConfigXML(string configFilePath)
        {
            return (Models.Userconfig)SerializationHelper.Load(typeof(Models.Userconfig), configFilePath);
        }

        /// <summary>
        /// 写入站点配置文件
        /// </summary>
        private static Models.Userconfig saveConifgXML(Models.Userconfig model, string configFilePath)
        {
            lock (lockHelper)
            {
                SerializationHelper.Save(model, configFilePath);
            }
            return model;
        }
        #endregion
    }
}
